package com.mit.wx.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author 郭文梁
 * <p>Create by 郭文梁</p>
 * <p>FOR: Wechart Util</p>
 */
@Slf4j
public class WechartUtil {
    public static boolean checkSignature(String signature, String token, String nonce, String timestamp) {
        String[] params = new String[3];
        params[0] = token;
        params[1] = nonce;
        params[2] = timestamp;
        Arrays.sort(params);

        StringBuilder sb = new StringBuilder();
        for (String s : params) {
            sb.append(s);
        }
        String sorted = sb.toString();
        log.info("sorted={}", sorted);
        String sign = DigestUtils.sha1Hex(sorted);
        log.info("sha1 result={}", sign);
        return Objects.equals(sign, signature);
    }
}
